//
//  XQLoginPhoneView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/20.
//

#import "XQLoginPhoneView.h"

@interface XQLoginPhoneView()
@property (nonatomic,strong) UILabel *phoneLb;
@property (nonatomic,strong) UIView *phoneView;
@property (nonatomic,strong) UIImageView *phoneImageView;
@property (nonatomic,strong) UILabel *phonePrefixLb;
@property (nonatomic,strong) UILabel *phoneLineLb;
@property (nonatomic,strong) UITextField *phoneTextField;
@end

@implementation XQLoginPhoneView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.phoneLb];
    [self addSubview:self.phoneView];
    [self.phoneView addSubview:self.phoneImageView];
    [self.phoneView addSubview:self.phonePrefixLb];
    [self.phoneView addSubview:self.phoneLineLb];
    [self.phoneView addSubview:self.phoneTextField];
}

- (void)makeConstraints {
    [self.phoneLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@18);
        make.height.mas_equalTo(28);
    }];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneLb.mas_bottom).offset(10);
        make.left.equalTo(@17);
        make.right.equalTo(@-28);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(@0);
    }];
    [self.phoneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerY;
        make.left.equalTo(@9);
        make.width.height.equalTo(@24);
    }];
    [self.phonePrefixLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.phoneImageView);
//        make.width.mas_equalTo(38);
        make.left.equalTo(self.phoneImageView.mas_right);
    }];
    [self.phoneLineLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.phoneImageView);
        make.width.equalTo(@14);
        make.left.equalTo(self.phonePrefixLb.mas_right);
    }];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.phoneImageView);
        make.left.equalTo(self.phoneLineLb.mas_right);
        make.right.equalTo(@0);
        make.top.bottom.equalTo(@0);
    }];
}

#pragma mark - private

// 监听文本变化的方法
- (void)textFieldDidChange:(UITextField *)textField {
    if (self.textFielHandle) {
        self.textFielHandle(textField.text);
    }
}

#pragma mark - lazy

- (UILabel *)phoneLb {
    if (!_phoneLb) {
        _phoneLb = [[UILabel alloc] init];
        _phoneLb.text = @"Mobile number";
        _phoneLb.textColor = kMainTextColor;
        _phoneLb.font = [UIFont systemFontOfSize:20];
    }
    return _phoneLb;
}

- (UIView *)phoneView {
    if (!_phoneView) {
        _phoneView = [[UIView alloc] init];
        _phoneView.layer.cornerRadius = 15;
        _phoneView.backgroundColor = [UIColor whiteColor];
    }
    return _phoneView;
}

- (UIImageView *)phoneImageView {
    if (!_phoneImageView) {
        _phoneImageView = [[UIImageView alloc] init];
        [_phoneImageView sd_setImageWithURL:[NSURL URLWithString:[XQToolManager manager].countryLogo]];
    }
    return _phoneImageView;
}

- (UILabel *)phonePrefixLb {
    if (!_phonePrefixLb){
        _phonePrefixLb = [[UILabel alloc] init];
        _phonePrefixLb.text = [NSString stringWithFormat:@"%@",[XQToolManager manager].phoneScenarios];
        _phonePrefixLb.textColor = [UIColor colorWithHexString:@"#4C1DEE"];
        _phonePrefixLb.font = [UIFont boldSystemFontOfSize:20];
        
        [_phonePrefixLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [_phonePrefixLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _phonePrefixLb;
}

- (UILabel *)phoneLineLb {
    if (!_phoneLineLb) {
        _phoneLineLb = [[UILabel alloc] init];
        _phoneLineLb.textColor = [UIColor colorWithHexString:@"#999999"];
        _phoneLineLb.text = @"｜";
        _phoneLineLb.font = [UIFont systemFontOfSize:14];
    }
    return _phoneLineLb;
}

- (UITextField *)phoneTextField {
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc] init];
        _phoneTextField.font = [UIFont boldSystemFontOfSize:20];
        _phoneTextField.textAlignment = NSTextAlignmentLeft;
        _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        _phoneTextField.textColor = kBlackTextColor;
        _phoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Enter your phone number" attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#999999"]}];
        [_phoneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 
    }
    return _phoneTextField;
}



@end
